﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using AtletIS.Shared.Data;
using AtletIS.Shared.Entities;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Admin
{
    public partial class frmAdminCompetitions : Form
    {
        private aDbDataSet.CompetitionsRow drActiveCompetition;
        private bool bNew;
        
        // Constructor
        public frmAdminCompetitions()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;

            this.nudYear.Value = DateTime.Now.Year;
        }

        // Load
        private void frmAdminCompetitions_Load(object sender, EventArgs e)
        {
            this.tsbCancel.Enabled = false;
            this.tsbSave.Enabled = false;

            DatabaseHelper.FillCompetitionsByYear(this.aDbDataSet.Competitions, (int)this.nudYear.Value);
        }

        // Year changed
        private void nudYear_ValueChanged(object sender, EventArgs e)
        {
            DatabaseHelper.FillCompetitionsByYear(this.aDbDataSet.Competitions, (int)this.nudYear.Value);
        }

        // Selection changed
        private void competitionsDataGridView_SelectionChanged(object sender, EventArgs e)
        {
            if (competitionsDataGridView.CurrentRow == null)
                return;

            try
            {
                DataRowView drv = (DataRowView)competitionsDataGridView.CurrentRow.DataBoundItem;
                chkActive.Checked = Convert.ToBoolean(drv["Active"]);
                txtName.Text = drv["Name"].ToString();
                txtCity.Text = drv["City"].ToString();
                txtOrganiser.Text = drv["Organiser"].ToString();
                dtpDateFrom.Value = Convert.ToDateTime(drv["DateFrom"]);
                dtpDateTo.Value = Convert.ToDateTime(drv["DateTo"]);
                dtpDeadline.Value = Convert.ToDateTime(drv["Deadline"]);
                dtpStartline.Value = Convert.ToDateTime(drv["Startline"]);
                chkClubsCountries.Checked = Convert.ToBoolean(drv["ClubsCountries"]);
                nudLanesCount.Value = Convert.ToInt32(drv["Lanes"]);
                txtLanesQuality.Text = drv["LanesQuality"].ToString();
                nudMeasurementId.Value = Convert.ToInt32(drv["MeasurementId"]);
                nudWebId.Value = Convert.ToInt32(drv["WebId"]);
                chkIndoor.Checked = Convert.ToBoolean(drv["Indoor"]);
                chkElectronic.Checked = Convert.ToBoolean(drv["Electronic"]);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Preteky", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // New
        private void tsbNew_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = true;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbActive.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;
                tsbDisciplineList.Enabled = false;

                chkActive.Checked = false;
                txtName.Text = string.Empty;
                txtOrganiser.Text = string.Empty;
                txtCity.Text = string.Empty;

                dtpDateFrom.Value = DateTime.Now.Date;
                dtpDateTo.Value = DateTime.Now.Date;
                dtpDeadline.Value = DateTime.Now.Date;
                dtpStartline.Value = DateTime.Now.Date;

                chkClubsCountries.Checked = true;
                nudLanesCount.Value = 8;
                txtLanesQuality.Text = "4,5,3,6,2,7,1,8";

                chkIndoor.Checked = false;
                chkElectronic.Checked = true;
                nudMeasurementId.Value = 1;
                nudWebId.Value = 0;

                this.drActiveCompetition = aDbDataSet.Competitions.NewCompetitionsRow();

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Preteky - pridať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Edit
        private void tsbEdit_Click(object sender, EventArgs e)
        {
            try
            {
                bNew = false;
                tsbNew.Enabled = false;
                tsbEdit.Enabled = false;
                tsbActive.Enabled = false;
                tsbCancel.Enabled = true;
                tsbSave.Enabled = true;
                tsbDisciplineList.Enabled = false;

                DataRowView drv = (DataRowView)competitionsDataGridView.CurrentRow.DataBoundItem;
                this.drActiveCompetition = (aDbDataSet.CompetitionsRow)drv.Row;

                gbList.Enabled = false;
                gbDetails.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Preteky - editovať", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Cancel
        private void tsbCancel_Click(object sender, EventArgs e)
        {
            tsbNew.Enabled = true;
            tsbEdit.Enabled = true;
            tsbActive.Enabled = true;
            tsbCancel.Enabled = false;
            tsbSave.Enabled = false;
            tsbDisciplineList.Enabled = true;

            gbList.Enabled = true;
            gbDetails.Enabled = false;

            competitionsDataGridView_SelectionChanged(null, null);
        }

        // Save
        private void tsbSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtName.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte názov!", "Kluby - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtCity.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Zadajte miesto!", "Preteky - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                if (txtLanesQuality.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Vyplňte výhodnosť dráh!", "Preteky - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }

                this.drActiveCompetition["Active"] = chkActive.Checked;
                this.drActiveCompetition["Name"] = txtName.Text;
                this.drActiveCompetition["City"] = txtCity.Text;
                this.drActiveCompetition["Organiser"] = txtOrganiser.Text;

                this.drActiveCompetition["DateFrom"] = dtpDateFrom.Value;
                this.drActiveCompetition["DateTo"] = dtpDateTo.Value;
                this.drActiveCompetition["Deadline"] = dtpDeadline.Value;
                this.drActiveCompetition["Startline"] = dtpStartline.Value;

                this.drActiveCompetition["ClubsCountries"] = chkClubsCountries.Checked;
                this.drActiveCompetition["Lanes"] = nudLanesCount.Value;
                this.drActiveCompetition["LanesQuality"] = txtLanesQuality.Text;
                this.drActiveCompetition["MeasurementId"] = nudMeasurementId.Value;
                this.drActiveCompetition["Indoor"] = chkIndoor.Checked;
                this.drActiveCompetition["Electronic"] = chkElectronic.Checked;

                this.drActiveCompetition["WebId"] = nudWebId.Value;
                this.drActiveCompetition["Code"] = nudCode.Value;
                this.drActiveCompetition["Modified"] = DateTime.Now;

                if (this.drActiveCompetition["Sync"] == DBNull.Value)
                {
                    this.drActiveCompetition["Sync"] = false;
                }

                if (bNew)
                {
                    aDbDataSet.Competitions.Rows.Add(this.drActiveCompetition);
                    this.competitionsTableAdapter.Update(aDbDataSet.Competitions);
                }
                else
                {
                    this.competitionsTableAdapter.Update(this.drActiveCompetition);
                }

                DatabaseHelper.FillCompetitionsByYear(this.aDbDataSet.Competitions, (int)this.nudYear.Value);

                tsbNew.Enabled = true;
                tsbEdit.Enabled = true;
                tsbActive.Enabled = true;
                tsbCancel.Enabled = false;
                tsbSave.Enabled = false;
                tsbDisciplineList.Enabled = true;

                gbDetails.Enabled = false;
                gbList.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + ex.StackTrace, "Preteky - uloženie", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // Close
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // Discipline list
        private void OpenDisciplineList()
        {
            try
            {
                if (competitionsDataGridView.CurrentRow != null)
                {
                    DataRowView drv = (DataRowView)competitionsDataGridView.CurrentRow.DataBoundItem;
                    CompetitionEntity competition = CompetitionHelper.ConvertToEntity(drv.Row);

                    frmAdminDisciplineList frmDL = new frmAdminDisciplineList(competition);
                    frmDL.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                ExceptionHelper.ShowErrorInfo(ex);
            }
        }

        private void tsbDisciplineList_Click(object sender, EventArgs e)
        {
            this.OpenDisciplineList();
        }

        private void competitionsDataGridView_DoubleClick(object sender, EventArgs e)
        {
            this.OpenDisciplineList();
        }

        // Lanes count changed
        private void nudLanesCount_ValueChanged(object sender, EventArgs e)
        {
            if (!nudLanesCount.Enabled)
                return;

            switch (Convert.ToInt32(nudLanesCount.Value))
            {
                case 4: 
                    txtLanesQuality.Text = "2,3,1,4";
                    break;

                case 6:
                    txtLanesQuality.Text = "3,4,2,5,1,6";
                    break;

                case 8:
                    txtLanesQuality.Text = "4,5,3,6,2,7,1,8";
                    break;

                default:
                    break;

            }
        }

        // Set Active Competition
        private void tsbActive_Click(object sender, EventArgs e)
        {
            try
            {
                if (competitionsDataGridView.CurrentRow == null)
                    return;

                int competitionID = Convert.ToInt32(((DataRowView)this.competitionsDataGridView.CurrentRow.DataBoundItem)["ID"]);

                DataTable dtAll = competitionsTableAdapter.GetData();
                foreach (DataRow drComp in dtAll.Rows)
                {
                    if (Convert.ToInt32(drComp["ID"]) == competitionID)
                        drComp["Active"] = true;
                    else
                        drComp["Active"] = false;
                }

                // Save + Reload
                this.competitionsTableAdapter.Update((aDbDataSet.CompetitionsDataTable)dtAll);
                this.frmAdminCompetitions_Load(null, null);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Nastaviť ako aktívne", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        

        

        




    }
}
